<?php

namespace app\common\validate\driving;

use think\Validate;

class Leave extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'type' => 'require|gt:0',
        'start_time' => 'require|check_start_time',
        'end_time' => 'require|check_end_time',
        'desc' => 'require',
    ];
    /**
     * 提示消息
     */
    protected $message = [
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add' => [],
        'edit' => [],
        'create' => ['type', 'start_time', 'end_time','desc'],
    ];

    /**
     * 构造函数
     * @access public
     * @param array $rules 验证规则
     * @param array $message 验证提示信息
     * @param array $field 验证字段描述信息
     */
    public function __construct(array $rules = [], $message = [], $field = [])
    {
        $this->field = [
            'type' => '请假类型',
            'start_time' => '开始时间',
            'end_time' => '结束时间',
            'desc' => '请假事由',
        ];
        $this->message = array_merge($this->message, [
            'start_time.check_start_time' => '开始时间必须大于当前时间',
            'end_time.check_end_time' => '结束时间必须大于开始时间',
        ]);
        parent::__construct($rules, $message, $field);
    }

    protected function check_start_time($value, $rule, $data)
    {
        if ($value <= date('Y-m-d H:i')) {
            return false;
        }
        return true;
    }

    protected function check_end_time($value, $rule, $data)
    {
        if ($data['start_time'] >= $value) {
            return false;
        }
        return true;
    }
}
